package com.ad.pet.app.shopcar.confirm;

import android.app.Dialog;
import android.content.Intent;
import android.graphics.Paint;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.Switch;
import android.widget.TextView;

import com.ad.pet.Logger;
import com.ad.pet.MainActivity;
import com.ad.pet.R;
import com.ad.pet.app.Navigation;
import com.ad.pet.app.RxDisposable;
import com.ad.pet.app.mall.adapter.CounpousAdapter;
import com.ad.pet.app.shopcar.DaggerShoppingCarCompent;
import com.ad.pet.app.shopcar.adapter.ShippingWayAdapter;
import com.ad.pet.app.shopcar.confirm.adapter.ConfirmGoodAdaper;
import com.ad.pet.common.data.utils.SpUtil;
import com.ad.pet.common.data.utils.glide.GlideUtil;
import com.ad.pet.common.data.utils.ui.ToastUtil;
import com.ad.pet.common.structure.events.Events;
import com.ad.pet.common.structure.events.RxBus;
import com.ad.pet.common.structure.mvp.BasePresenter;
import com.ad.pet.common.structure.ui.activity.AppToolbarActivity;
import com.ad.pet.constant.Constant;
import com.ad.pet.models.remote.CommList;
import com.ad.pet.models.remote.account.Banlance;
import com.ad.pet.models.remote.account.UserBuyReturn;
import com.ad.pet.models.remote.account.order.PayCompleted;
import com.ad.pet.models.remote.account.order.SignCheck;
import com.ad.pet.models.remote.address.Address;
import com.ad.pet.models.remote.cart.BigShipingw;
import com.ad.pet.models.remote.cart.Coupons;
import com.ad.pet.models.remote.cart.Good;
import com.ad.pet.models.remote.cart.ShippingWay;
import com.ad.pet.models.remote.mall.Counpous;
import com.ad.pet.ui.ConpounsDIalog;
import com.ad.pet.ui.SimpleDividerDecoration;
import com.ad.pet.ui.TimeLabelZTextview;
import com.ad.pet.utils.LyxUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;

import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;
import rx.functions.Action1;

/**
 * Created by SEELE on 2018/10/9.
 */

public class ConfirmStyleOrderActivity extends AppToolbarActivity implements ConfirmOrderContracts.View{


    @BindView(R.id.tv_confirm)
    TextView mTvConfirm;
    @BindView(R.id.recyclerShop)
    RecyclerView mRecyclerShop;

    @Inject
    RxDisposable mRxDisposable;
    @Inject
    ConfirmOrderPresenter mConfirmOrderPresenter;


    @BindView(R.id.tv_chose_address)
    TextView mTvChoseAddress;
    @BindView(R.id.tv_recive_people)
    TextView mTvRecivePeople;
    @BindView(R.id.tv_address)
    TextView mTvAddress;
    @BindView(R.id.linear_address)
    LinearLayout mLinearAddress;
    @BindView(R.id.relativeLayout_address)
    RelativeLayout mRelativeLayoutAddress;
    @BindView(R.id.tv_add_address)
    TextView mTvAddAddress;
    @BindView(R.id.tv_total)
    TextView mTvTotal;
    @BindView(R.id.tv_sub_total)
    TextView mTvSubTotal;
    @BindView(R.id.sb_packet)
    Switch mSbPacket;
    @BindView(R.id.tv_redpacket)
    TextView mTvRedpacket;
    @BindView(R.id.sb_money)
    Switch mSbMoney;
    @BindView(R.id.tv_just_money)
    TextView mTvJustMoney;
    @BindView(R.id.recycler_ship)
    RecyclerView mRecyclerShip;
    @BindView(R.id.linear_ship)
    LinearLayout mLinearShip;
    @BindView(R.id.tv_end_total)
    TextView mTvEndTotal;
    @BindView(R.id.iv_green_gou)
    ImageView mIvGreenGou;
    @BindView(R.id.tv_30Day)
    TextView mTv30Day;
    @BindView(R.id.iv_refunds)
    ImageView mIvRefunds;
    @BindView(R.id.iv_triff)
    ImageView mIvTriff;
//    @BindView(R.id.tv_triffcontent)
//    TextView mTvTriffcontent;
//    @BindView(R.id.iv_triff_why)
//    ImageView mIvTriffWhy;

    String mShipWayId = "1";
    String mShipFee = "0";
    String mShipCurruncy = "0";
    @BindView(R.id.tv_note)
    TextView mTvNote;
    @BindView(R.id.iv_down)
    ImageView mIvDown;
    @BindView(R.id.linear_note)
    LinearLayout mLinearNote;
    @BindView(R.id.relativeLayout_notes)
    RelativeLayout mRelativeLayoutNotes;
    @BindView(R.id.tv_train_pay)
    TextView mTvTrainPay;
    @BindView(R.id.relativeLayout_price)
    RelativeLayout mRelativeLayoutPrice;
    @BindView(R.id.tv_counpous)
    TextView mTvCounpous;
    @BindView(R.id.tv_orgin_price)
    TextView mTvOrginPrice;
    @BindView(R.id.tv_total_reduce)
    TextView mTvTotalReduce;
    @BindView(R.id.linear_reduce)
    LinearLayout mLinearReduce;
    @BindView(R.id.tv_bazhe)
    TimeLabelZTextview mTvBazhe;
    @BindView(R.id.linear_bazhe)
    RelativeLayout mLinearBazhe;
//    @BindView(R.id.linear_canget)
//    LinearLayout mLinearCanget;
    @BindView(R.id.tv_jiesheng)
    TextView mTvJieSheng;

    private String mCartIds;
    //    private CouponsAdaper mCouponsAdaper;
//    private ConfirmShopAdapter mShopAdapter;
    private LinearLayoutManager mLinearLayoutManager;


//
//    Coupons mCouponsCode;

    @BindView(R.id.tv_other_money)
    TextView mTvOtherMoney;


    @BindView(R.id.tv_Phone)
    TextView mTvPhone;

    @BindView(R.id.tv_zip)
    TextView mTvZip;

    private static final int ADDRESS_RESQULT_CODE = 11;
    private Address mAddress;
    @BindView(R.id.iv_right)
    ImageView mIvRigth;
    private List<Good> mGoods;
    private String mPrice;

    @BindView(R.id.tv_count_red)
    TextView mTvCountRed;

    @BindView(R.id.tv_just_money_count)
    TextView mTvJustCountMoney;
    private ShippingWayAdapter mShippingwayAdapter;
    private Banlance mBanlance;

    private double mPacketPrice = 0.0d;//可用的红包
    private double mJustPrice = 0.0d;//用了多少余额
    private String mOrderSn;


    @BindView(R.id.linear_confirm)
    LinearLayout mLinearLayoutConfirm;
//
//    @BindView(R.id.tv_can_get)
//    TextView mTvCanGet;

    @BindView(R.id.relativeLayout_counpus)
    RelativeLayout mRelativeLayoutCounpus;
    private Dialog mConpounsDialog;
    private RecyclerView mRecyclerCounpus;


    private CounpousAdapter mCounpousAdapter;
    private List<Counpous> mCounpous;
    private String mUseCoupons = "0";
    private String mUseCouponsId = "0";

    @BindView(R.id.tv_count_counppous)
    TextView mTvCoupons;

    @BindView(R.id.iv_head)
    ImageView mIvHead;

    @BindView(R.id.linear_new_user)
    LinearLayout mLinearLayoutUser;

    @BindView(R.id.tv_name)
    TextView mTvName;

    @BindView(R.id.tv_attribute)
    TextView mTvAtrbute;

    @BindView(R.id.tv_price)
    TextView tvPrice;

    @BindView(R.id.tv_only_time)
    TextView mTvOnlyTime;


    CommList mCommList;


    @Override
    protected void init(Bundle savedInstanceState) {
        super.init(savedInstanceState);
        mStatusLayoutManager.showContent();
        mToolbar.setTitle(getString(R.string.confirm_order));
    }

    @Override
    protected int contentLayout() {
        return R.layout.activity_confirm_style_order;
    }

    @Override
    protected void clickRetry(View v) {

    }

    @Override
    protected void initViews() {
        mConfirmOrderPresenter.onCreate(this);

        mCartIds = getIntent().getStringExtra(Constant.CART_IDS);
        mGoods = (List<Good>) getIntent().getSerializableExtra(Constant.CART_ID);
        mPrice = getIntent().getStringExtra(Constant.START_PRICE);
        mCommList = (CommList) getIntent().getSerializableExtra(Constant.NAME);

        ConfirmGoodAdaper goodAdaper = new ConfirmGoodAdaper(mGoods);
        mLinearLayoutManager = new LinearLayoutManager(mActivity);
        mRecyclerShop.setNestedScrollingEnabled(false);
        mRecyclerShop.addItemDecoration(new SimpleDividerDecoration(mActivity, 15, R.color.bg_color));
        mRecyclerShop.setLayoutManager(mLinearLayoutManager);
        mRecyclerShop.setAdapter(goodAdaper);


        mLinearLayoutUser.setVisibility("1".equals(mCommList.getIs_show_new_user_gift()) ? View.VISIBLE : View.GONE);
        mTvName.setText(getString(R.string.new_user_lingshi));
        mTvAtrbute.setText(getString(R.string.if_not_good));
        Logger.e("head:" + mCommList.getNew_user_gift_img());
        GlideUtil.load(this, mCommList.getNew_user_gift_img(), mIvHead);
        tvPrice.setText(SpUtil.getInstance().getString(Constant.CURRENCY_LABEL, "US $") + "0.0");


        mTvSubTotal.setText(SpUtil.getInstance().getString(Constant.CURRENCY_LABEL, "US $") + mPrice);

        mConfirmOrderPresenter.get_user_balance();

        int count = 0;
        for (Good good :
                mGoods) {
            count = good.getQuantity() + count;
        }


        mRecyclerShip.setLayoutManager(new LinearLayoutManager(mActivity));
        mShippingwayAdapter = new ShippingWayAdapter(null);
        mRecyclerShip.setAdapter(mShippingwayAdapter);

        mConfirmOrderPresenter.getShipingWays(String.valueOf(count), mPrice);
        mTvOtherMoney.setText(SpUtil.getInstance().getString(Constant.CURRENCY_LABEL, "US $") + mPrice);
        mTvEndTotal.setText(SpUtil.getInstance().getString(Constant.CURRENCY_LABEL, "US $") + mPrice + getUsEnd(Double.valueOf(mPrice)));
//        mTvCanGet.setText(SpUtil.getInstance().getString(Constant.CURRENCY_LABEL, "US $") + mPrice);

        mConpounsDialog = new ConpounsDIalog(mActivity, R.style.custom_dialog_style);
        mRecyclerCounpus = mConpounsDialog.findViewById(R.id.recycler_counpus);


//        mCounpousAdapter = new CounpousAdapter(null);
//        mRecyclerCounpus.setLayoutManager(new LinearLayoutManager(mActivity));
//        mRecyclerCounpus.setAdapter(mCounpousAdapter);


        if (mCommList.getIs_show_first_order_return() == 1) {
            mTvOrginPrice.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
            mTvOrginPrice.setText(SpUtil.getInstance().getString(Constant.CURRENCY_LABEL, "US $") + mCommList.getOrig_total_price());
            mTvTotalReduce.setText(SpUtil.getInstance().getString(Constant.CURRENCY_LABEL, "US $") + mCommList.getMoney_saved());
//            mLinearCanget.setVisibility(View.GONE);
            mLinearBazhe.setVisibility(View.VISIBLE);
            mTvOnlyTime.setVisibility(View.VISIBLE);
            mTvBazhe.setDownTimeStart( mCommList.getFirst_order_return_residue_time(), new TimeLabelZTextview.onDownTimeLinstener() {
                @Override
                public void onTimeEnd() {
                    mLinearBazhe.setVisibility(View.GONE);
//                    mLinearCanget.setVisibility(View.VISIBLE);

                }
            });
            mTvJieSheng.setVisibility(View.VISIBLE);
            mTvBazhe.setHaoColor(R.color.level1);
            mTvBazhe.setTimeBgColor(R.color.colorTransparent);
            mTvBazhe.setTimeColor(R.color.yellow);
        } else {
            mTvOrginPrice.setVisibility(View.GONE);
            mTvTotalReduce.setVisibility(View.GONE);
//            mLinearCanget.setVisibility(View.VISIBLE);
            mLinearBazhe.setVisibility(View.GONE);
            mTvOnlyTime.setVisibility(View.GONE);
            mTvJieSheng.setVisibility(View.GONE);
        }

    }

    @Override
    protected void initData() {
//        mConfirmOrderPresenter.getCoupons(mCartIds);
    }

    @Override
    protected void initEvents() {
        mLinearLayoutConfirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (mAddress == null) {
                    ToastUtils.showShortToast(getString(R.string.please_chose_address_reciver));
                    return;
                }
                String banlance = "0";
                String redEn = "0";
                if (mBanlance != null) {

                    if (mSbMoney.isChecked()) {
//                        banlance = mBanlance.getBalance();
                        double d = LyxUtils.formatAdd(Double.valueOf(mBanlance.getBalance()), Double.valueOf(mBanlance.getBalance_red_envelope()));
                        banlance = String.valueOf(d);
                    }
                    if (mSbPacket.isChecked()) {

                        redEn = String.valueOf(mPacketPrice);
                    }
                }

//                mConfirmOrderPresenter.confirmOrder(mCartIds, mAddress.getId(), mShipWayId, banlance, redEn, mShipCurruncy, mUseCouponsId);

            }
        });


        mRelativeLayoutAddress.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                Navigation.goAddress(mActivity, mAddress, ADDRESS_RESQULT_CODE);
            }
        });


        mShippingwayAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                List<ShippingWay> shippingWays = mShippingwayAdapter.getData();
                mShipWayId = shippingWays.get(position).getTransport_id();
                mShipFee = shippingWays.get(position).getFreight_usd();
                mShipCurruncy = shippingWays.get(position).getFreight_now();
                for (int i = 0; i < shippingWays.size(); i++) {
                    shippingWays.get(i).setCheck(position == i);
                    if (position == i) {
                        if (mSbMoney.isChecked()) {
                            mTvJustCountMoney.setText(getBalanceEndPrice());
                        }
                    }
                }
                mTvOtherMoney.setText(getEndPrice());
                mTvEndTotal.setText(getEndPrice());
                mShippingwayAdapter.notifyDataSetChanged();
            }
        });

        mSbMoney.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {


                if (mBanlance != null) {

                    if (Double.valueOf(mJustPrice) == 0) {
                        mSbMoney.setChecked(false);
                        return;
                    }

                    if (isChecked) {
                        mTvJustCountMoney.setText(getBalanceEndPrice());
                    } else {
                        mTvJustCountMoney.setText("0.00");
                    }
                    mTvOtherMoney.setText(getEndPrice());
                    mTvEndTotal.setText(getEndPrice());
                    setReturnPrice();
                }

            }
        });

        mSbPacket.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {


                if (mBanlance != null) {
                    if (Double.valueOf(mPacketPrice) == 0) {
                        mSbPacket.setChecked(false);
                        return;
                    }
                    if (isChecked) {
                        mTvCountRed.setText("-" + SpUtil.getInstance().getString(Constant.CURRENCY_LABEL, "US $") + mPacketPrice);
                    } else {
                        mTvCountRed.setText("0.00");
                    }
                    if (mSbMoney.isChecked()) {
                        mTvJustCountMoney.setText(getBalanceEndPrice());
                    }
                    mTvOtherMoney.setText(getEndPrice());
                    mTvEndTotal.setText(getEndPrice());
                    setReturnPrice();
                }
            }
        });

        mIvRefunds.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Navigation.goCommonWebView(mActivity, getString(R.string.returns_and_refunds_policy), LyxUtils.muchLuanguageUrl("/about/refundspolicy.html"));
            }
        });


        mRelativeLayoutCounpus.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (mCounpous == null) {
                    ToastUtil.getToastUtil().showShort(getString(R.string.not_coupouns_can_use));
                    return;
                }

                mConpounsDialog.show();


            }
        });


//        mCounpousAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
//            @Override
//            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
//                List<Counpous> useCoupuouns = mCounpousAdapter.getData();
//                if (useCoupuouns.get(position).isCan()) {
//                    for (int i = 0; i < useCoupuouns.size(); i++) {
//
//                        useCoupuouns.get(i).setChose(i == position);
//
//                    }
//                    mUseCoupons = useCoupuouns.get(position).getCurrency_value();
//                    mTvCoupons.setText("-" + SpUtil.getInstance().getString(Constant.CURRENCY_LABEL, "US $") + mUseCoupons);
//                    mUseCouponsId = useCoupuouns.get(position).getId();
//                    mTvOtherMoney.setText(getEndPrice());
//                    mTvEndTotal.setText(getEndPrice());
//                    mConpounsDialog.dismiss();
//                }
//                setReturnPrice();
//                mCounpousAdapter.notifyDataSetChanged();
//            }
//        });


        mRxDisposable.add(RxBus.getDefault().toObservable(Address.class)
                .subscribe(new Action1<Address>() {
                    @Override
                    public void call(Address s) {
                        mAddress = s;
                        if (mAddress != null) {
                            mIvRigth.setVisibility(View.VISIBLE);
                            mTvAddAddress.setVisibility(View.GONE);
                            mLinearAddress.setVisibility(View.VISIBLE);
                            mTvChoseAddress.setVisibility(View.VISIBLE);
//                            mTvRecivePeople.setText(mAddress.getFirstname() + " " + mAddress.getLastname());
//                            mTvPhone.setText(mAddress.getPhone());
//                            mTvZip.setText(mAddress.getPostcode());
//                            mTvAddress.setText(mAddress.getCountry_name() + " " + mAddress.getState_name() + " " + mAddress.getCity() + " " + mAddress.getAddress());
                        }
                    }
                }));

    }

    /**
     * 设置返回金额
     */
    private void setReturnPrice() {
        double canGet = 0;
        if (mSbPacket.isChecked()) {
            canGet = LyxUtils.formatSubtract(Double.valueOf(mPrice), getPriceText(mTvCountRed.getText().toString()));
        } else {
            canGet = Double.valueOf(mPrice);
        }
        canGet = LyxUtils.formatSubtract(canGet, Double.valueOf(mUseCoupons));
        if (canGet < 0) {
            canGet = 0;
        }
//        mTvCanGet.setText(SpUtil.getInstance().getString(Constant.CURRENCY_LABEL, "US $") + String.valueOf(canGet));
    }


    @Override
    public void initInject() {
        DaggerShoppingCarCompent.builder().applicationComponent(getApplicationComponent())
                .activityModule(getActivityModule()).build().inject(this);
    }

    @Override
    protected BasePresenter getPresenter() {
        return mConfirmOrderPresenter;
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void error(String msg) {

        ToastUtil.getToastUtil().showShort(msg);

    }

    @Override
    public void error(int code, String msg) {

        ToastUtil.getToastUtil().showShort(msg);

    }

    @Override
    public void getCouponsSuccess(List<Counpous> list) {

//        mCounpous = coupons.getList() == null ? null : coupons.getList().getAvailable_list();
//
//        if (mCounpous == null) {
//            mCounpous = new ArrayList<>();
//
//        }
//        Counpous noUse = new Counpous();
////        noUse.setChose(false);
////        noUse.setId("0");
//        noUse.setValue(getString(R.string.no_use));
//        noUse.setCurrency_value("0.00");
//        noUse.setValue("0.00");
//        if (mCounpous.size() != 0) {
//            mCounpous.add(0, noUse);
//        } else {
//            mCounpous.add(noUse);
//        }


//        for (Counpous counpous1 :
//                mCounpous) {
////            counpous1.setCan(true);
//        }
//        List<Counpous> unCoupous = coupons.getList() == null ? null : coupons.getList().getUnavailable_list();
//        if (unCoupous != null) {
//            Counpous counpousText = new Counpous();
////            counpousText.setCoupon_type(coupons.getCoupon_spend_extra());
////            counpousText.setItemType(1);
//
//            mCounpous.add(counpousText);
//
//            mCounpous.addAll(unCoupous);
//
//        }
//
//        mCounpousAdapter.setNewData(mCounpous);


    }

    @Override
    public void checkCouponsSuccess(Coupons coupons) {

    }

    @Override
    public void confirmOrderSuccess(PayCompleted shop) {
        RxBus.getDefault().post("refresh_carts");
        mOrderSn = shop.getOrder_sn();
        String banlance = "0";
        String redEn = "0";
        if (mBanlance != null) {

            if (mSbMoney.isChecked()) {
//                banlance = mBanlance.getBalance();
                double d = LyxUtils.formatAdd(Double.valueOf(mBanlance.getBalance()), Double.valueOf(mBanlance.getBalance_red_envelope()));
                banlance = String.valueOf(d);
            }
            if (mSbPacket.isChecked()) {
                redEn = String.valueOf(mPacketPrice);
            }
        }
        if (TextUtils.isEmpty(shop.getBalance_sign())) {
            Navigation.goRecharge(mActivity, mOrderSn, MainActivity.REQUEST_GO_MALL);
            RxBus.getDefault().post(new Events.AddCarts());
            finish();
        } else {
            mConfirmOrderPresenter.paymentBalance(shop.getOrder_sn(), shop.getBalance_sign(), banlance, redEn);
        }

    }

    @Override
    public void getUserBanceSuccess(Banlance banlance) {
        mBanlance = banlance;
        double redCount = Double.valueOf(mBanlance.getCurrency_red_envelope());
        double tenPrice = LyxUtils.formatMultiply(Double.valueOf(mPrice), 0.10d);
        mPacketPrice = (tenPrice > redCount ? redCount : tenPrice);
        mJustPrice = LyxUtils.formatAdd(Double.valueOf(banlance.getCurrency_balance()), Double.valueOf(banlance.getBalance_red_envelope()));

        mTvRedpacket.setText(getString(R.string.red_packet) + "(" + SpUtil.getInstance().getString(Constant.CURRENCY_LABEL, "US $") + mPacketPrice + ")");
        mTvJustMoney.setText(getString(R.string.just_money) + "(" + SpUtil.getInstance().getString(Constant.CURRENCY_LABEL, "US $") + mJustPrice + ")");
        if (0d!=mJustPrice){
            mSbMoney.setChecked(true);

            mTvJustCountMoney.setText(getBalanceEndPrice());
            mTvOtherMoney.setText(getEndPrice());
            mTvEndTotal.setText(getEndPrice());
            setReturnPrice();
        }
    }

    @Override
    public void getShipingWaysSuccess(BigShipingw shippingWay) {
        List<ShippingWay> list = shippingWay.getTransport();
        if (list != null && list.size() > 0) {
            list.get(0).setCheck(true);
            mShipWayId = list.get(0).getTransport_id();
            mShipFee = list.get(0).getFreight_usd();
            mShipCurruncy = list.get(0).getFreight_now();
            mTvOtherMoney.setText(getEndPrice());
            mTvEndTotal.setText(getEndPrice());
            setReturnPrice();
        }
        mShippingwayAdapter.setNewData(list);
    }

    @Override
    public void paymentBalanceSuccess(int toPay) {

        if (toPay == 0) {

            Navigation.goCompleteOrder(mActivity, mOrderSn);
            RxBus.getDefault().post(new Events.AddCarts());
            finish();
        } else if (toPay == 1) {
            Navigation.goRecharge(mActivity, mOrderSn, MainActivity.REQUEST_GO_MALL);
            RxBus.getDefault().post(new Events.AddCarts());
            finish();
        }
    }

    @Override
    public void checkIsSignSuccess(SignCheck signCheck) {

    }

    @Override
    public void daySignSuccess(SignCheck signCheck) {

    }

    @Override
    public void sharH5UrlSuccess(SignCheck signCheck) {

    }

    @Override
    public void get_user_buy_return_success(UserBuyReturn userBuyReturn) {

    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (data == null)
            return;
        switch (requestCode) {

            case MainActivity.REQUEST_GO_MALL:
                setResult(RESULT_OK, data);
                finish();

                break;

            case ADDRESS_RESQULT_CODE:
                mAddress = (Address) data.getSerializableExtra("address");
                if (mAddress != null) {
                    mIvRigth.setVisibility(View.VISIBLE);
                    mTvAddAddress.setVisibility(View.GONE);
                    mLinearAddress.setVisibility(View.VISIBLE);
                    mTvChoseAddress.setVisibility(View.VISIBLE);
//                    mTvRecivePeople.setText(mAddress.getFirstname() + " " + mAddress.getLastname());
//                    mTvPhone.setText(mAddress.getPhone());
//                    mTvZip.setText(mAddress.getPostcode());
//                    mTvAddress.setText(mAddress.getCountry_name() + " " + mAddress.getState_name() + " " + mAddress.getCity() + " " + mAddress.getAddress());
                }

                break;
        }
    }

    @Override
    public void onDestroy() {

        if (mRxDisposable != null) {
            mRxDisposable.clear();
        }
        super.onDestroy();
    }


    private double getPriceText(String price) {
        price = price.replace(SpUtil.getInstance().getString(Constant.CURRENCY_LABEL, "US $"), "");
        price = price.replace("-", "");
        return LyxUtils.formatDouble2(Double.valueOf(price));
    }


    private String getEndPrice() {

        double end = Double.valueOf(mPrice) + Double.valueOf(mShipCurruncy);
        end = end - getPriceText(mTvCountRed.getText().toString()) - getPriceText(mTvJustCountMoney.getText().toString()) - Double.valueOf(mUseCoupons);
        if (end < 0) {
            end = 0.00d;
        }

        return SpUtil.getInstance().getString(Constant.CURRENCY_LABEL, "US $") + String.valueOf(LyxUtils.formatDouble2(end)) + getUsEnd(end);
    }

    /**
     * 获取余额
     */
    private String getBalanceEndPrice() {
        double end = LyxUtils.formatAdd(Double.valueOf(mPrice), Double.valueOf(mShipCurruncy));
        end = mJustPrice - end - getPriceText(mTvCountRed.getText().toString());
        if (end > 0) {
            end = LyxUtils.formatAdd(Double.valueOf(mPrice), Double.valueOf(mShipCurruncy)) - getPriceText(mTvCountRed.getText().toString());
        } else {
            end = mJustPrice;
        }

        return "-" + SpUtil.getInstance().getString(Constant.CURRENCY_LABEL, "US $") + LyxUtils.formatDouble2(end);
    }


    private String getUsEnd(double currenyMoney) {
        String end = "";

        if (!"US $".equals(SpUtil.getInstance().getString(Constant.CURRENCY_LABEL, "US $"))) {
            double rateRMB = Double.valueOf(SpUtil.getInstance().getString(Constant.CURRENCY_RATE));
            double rateUS = Double.valueOf(SpUtil.getInstance().getString(Constant.RMB_RATE));
            double rm = LyxUtils.formatMultiply(currenyMoney, rateRMB);
            double money = LyxUtils.formatDivide(rm, rateUS);
            end = "(US $" + money + ")";
        }
        return end;
    }




}
